package BLC

import (
	"flag"
	"fmt"
	"log"
	"os"
)

func (cli *Client) getBalanceCmd(getBalance *flag.FlagSet) {

	flagGetBalanceCmd := getBalance.String("from", "", "查询余额")

	err := getBalance.Parse(os.Args[2:])
	if err != nil {
		log.Panicf("get balance failed! %v\n ", err)
	}

	//查询余额
	if getBalance.Parsed() {
		if *flagGetBalanceCmd == "" {
			fmt.Println("未知的查询地址")
			PrintUsage()
			os.Exit(1)
		}
		cli.getBalance(*flagGetBalanceCmd)
	}
}

func (cli *Client) getBalance(from string) {

	if !dbExists() {
		fmt.Println("数据库不存在----")
		os.Exit(1)
	}

	blockChain := GetBlockChainObj()
	defer blockChain.CloseDB()
	fmt.Printf(" addr : %s  amount: %v  \n ", from, blockChain.getBalance(from))
}
